package com.zyg.seckill.controller;

import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.user.XxlSsoUser;
import com.zyg.common.utils.MyDate;
import com.zyg.seckill.entity.TbSeckillGoods;
import com.zyg.seckill.service.TbSeckillGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/11/25-15:07
 * ------------------------------
 */
@Controller
@RequestMapping
public class SeckillController {
    @Autowired
    private TbSeckillGoodsService seckillGoodsService;
    //1. 默认秒杀页面
    @GetMapping({"/","/seckill-index.html"})
    public String index(Model model){
        //1.0 得到需要取秒杀商品的时间段字符串
        Date date = MyDate.getDateMenus().get(0);
        String time = MyDate.getDateStr(date);
        System.out.println("time = " + time);

        //1.1 得到日期菜单
        List<Date> dateMenus = MyDate.getDateMenus();
        //1.2 放到model中
        model.addAttribute("menus",dateMenus);
        //1.3 得到秒杀商品列表
        List<TbSeckillGoods> seckillGoods = seckillGoodsService.findSecillGoods(time);
        //1.4 将得到的数据放到model中
        model.addAttribute("secKillGoodsList",seckillGoods);
        return "seckill-index";
    }

    //2. 根据商品id查询商品详情
    @GetMapping("/seckill/findGoodsById/{id}")
    public String  findGoodsById(@PathVariable String id,Model model){
        //2.1 根据商品id查询商品详情
        TbSeckillGoods seckillGoods = seckillGoodsService.getById(id);
        //2.2 将商品详情放到model中
        model.addAttribute("seckillGoods",seckillGoods);
        //2.3 返回视图
        return "seckill-item";
    }

    @GetMapping("getName")
    @ResponseBody
    public XxlSsoUser getName(HttpServletRequest request,Model model){
        XxlSsoUser ssoUser = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);
        return ssoUser;
    }


}
